package com.zhongkun.datahome.service.export.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: lifengsheng
 * @Date: 2025/5/27 11:04
 * @Desc 将斜杠转换为空
 */
public class StringDiagonalConverter implements Converter<String> {
    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (cellData == null || cellData.getType() == CellDataTypeEnum.EMPTY) {
            return null;
        }
        String stringValue = cellData.getStringValue();
        if (StringUtils.isBlank(stringValue) || "/".equals(stringValue.trim())) {
            return null;
        }
        return stringValue;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        WriteCellData<Object> cellData = new WriteCellData<>(CellDataTypeEnum.STRING);
        cellData.setStringValue(value);
        return cellData;
    }
}
